package com.zz.math;

import com.zz.node.ListNode;
import com.zz.node.Node;
import com.zz.util.ListNodeUtil;

import java.util.List;

public class L2Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode node = head;
        int flag = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 == null ? 0 : l1.val;
            int n2 = l2 == null ? 0 : l2.val;
            int num = n1 + n2 + flag;
            flag = num / 10;//要么是0，要么是1
            num %= 10;
            node.next = new ListNode(num);

            node = node.next;
            l1 = l1==null ? l1 : l1.next;
            l2 = l2==null ? l2 : l2.next;
        }
        if(flag==1){
            node.next = new ListNode(flag);
        }
        return head.next;
    }

    public int fan(ListNode  node){
        if(node != null){
            return node.val;
        }
        return 0;
    }

    public static void main(String[] args) {
        int[] arr = {2,4,6,1,7,8,10,9};
        ListNode head = ListNodeUtil.getListNodeForArr(arr);
        ListNodeUtil.printListNode(head);

    }
}
